package com.heima.page.listener;

import com.alibaba.fastjson.JSON;
import com.heima.common.constants.message.NewsAutoScanConstants;
import com.heima.page.service.GeneratePageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import java.util.Map;

@Component
@Slf4j
public class GeneratePageListener {

  @Autowired
  GeneratePageService generatePageService;


  @KafkaListener(topics = NewsAutoScanConstants.GENERATE_PAGE_TOPIC)
  public void recevieMessage(String message) {

    if (StringUtils.isNotBlank(message)) {
      log.info("*********GeneratePageListener receive message:{}", message);

      try {
        Map<String, Long> map = JSON.parseObject(message, Map.class);
        generatePageService.generateArticlePage(map.get("articleId"));
      } catch (Exception e) {
        e.printStackTrace();
        log.info("********* GeneratePage error ********* articleId：{}",message);
      }

      log.info("*********GeneratePage success*********");
    }

  }
}